<template>
    <div class="login-wrap">
        <el-form class="login-container"
                 :model="ruleForm" :rules="rules" ref="ruleForm">
            <!-- 头部信息-->
            <h3 class="title">用户登录</h3>
            <!-- 输入框input-->
            <el-form-item prop="uid">
                <el-input v-model="ruleForm.uid" type="text" placeholder="账号"/>
            </el-form-item>
            <el-form-item prop="password">
                <el-input v-model="ruleForm.password" type="password"
                          placeholder="密码"
                />
            </el-form-item>
            <el-row>
                <!-- 验证码输入框-->
                <el-col :span="12">
                    <el-form-item prop="captcha">
                        <el-input v-model="ruleForm.captcha" type="text"
                                  placeholder="验证码"
                                  @keyup.enter.native="submitForm('ruleForm')"
                        />
                    </el-form-item>
                </el-col>
                <!-- 验证码图片-->
                <el-col :span="12" style="text-align: right">
                    <img :src="codeImg" @click="getCode()"/>
                </el-col>
            </el-row>

            <el-form-item>
                <el-button type="primary" style="width: 100%"
                           :loading="logining"
                           @click="submitForm('ruleForm')"
                >
                    登录
                </el-button>

            </el-form-item>

        </el-form>


    </div>
</template>

<script>

    import {queryCaptcha, login} from '../api/loginApi';
    import encryptMD5 from 'js-md5';
    import {queryBalance,queryTrade,queryOrder,queryPosi} from '../api/orderApi';

    export default {
        name: "Login",
        data() {
            return {
                //1.提交表单
                ruleForm: {
                    uid: '',
                    password: '',
                    captcha: '',
                    captchaId: '',
                },
                //2.验证码图片
                codeImg: '',
                //3.限制规则
                rules: {
                    uid: [{required: true, message: '请输入账号', trigger: 'blur'}],
                    password: [{required: true, message: '请输入密码', trigger: 'blur'}],
                    captcha: [{required: true, message: '请输入验证码', trigger: 'blur'}],
                },
                //4.防止前端重复提交
                logining: false,
            }
        },
        //加载立刻执行
        created(){

            if(Boolean(this.$route.query.msg)){
                this.$message.info(this.$route.query.msg + "");
            }


          this.getCode();
        },
        methods: {
            //{id,imageBase64}
            captchaCallback(code, msg, captchaData) {
                this.ruleForm.captchaId = captchaData.id;
                this.codeImg = captchaData.imageBase64;
            },
            //common.js(网络交互) <-- logic.js(业务逻辑) <-- vue
            //验证码获取
            getCode() {
                queryCaptcha(this.captchaCallback);
            },

            //登录回调函数
            loginCallback(code, msg, acc) {
                if (code == 2) {
                    //登录失败
                    this.$message.error(msg);
                    this.logining = false;
                    this.getCode();
                } else {
                    //登录成功 uid token
                    sessionStorage.setItem("uid", acc.uid);
                    sessionStorage.setItem("token", acc.token);
                    //显示上次成功登录时间
                    if (acc.lastLoginDate.length > 1) {
                        this.$message.success("登录成功,上次登录时间:"
                            + acc.lastLoginDate + " " + acc.lastLoginTime);
                    } else {
                        this.$message.success("登录成功");
                    }
                    //跳转主页面
                    setTimeout(() => {
                        this.logining = false;
                        this.$router.push({path : '/dashboard'});
                        //成交 委托 持仓查询
                        queryBalance();
                        queryOrder();
                        queryTrade();
                        queryPosi();
                    }, 1000);

                }
            },
            //提交表单
            submitForm(formName) {
                this.$refs[formName].validate(valid => {
                    if (valid) {
                        this.logining = true;
                        login({
                            uid: this.ruleForm.uid,
                            password: encryptMD5(this.ruleForm.password),
                            captcha: this.ruleForm.captcha,
                            captchaId: this.ruleForm.captchaId,
                        }, this.loginCallback);
                    } else {
                        this.$message.error('用户名/密码/验证码不能为空');
                        this.logining = false;
                    }
                })
            }
        }
    }
</script>

<style scoped>
    .login-wrap {
        box-sizing: border-box;
        width: 100%;
        height: 100%;
        padding-top: 10%;
        background-image: url();
        background-repeat: no-repeat;
        background-position: center right;
        background-size: 100%;
    }

    .login-container {
        border-radius: 10px;
        margin: 0px auto;
        width: 350px;
        padding: 30px 35px 15px 35px;
        background: #fff;
        border: 1px solid #eaeaea;
        text-align: left;
        box-shadow: 0 0 20px 2px rgba(0, 0, 0, 0.1);
    }

    .title {
        margin: 0px auto 40px auto;
        text-align: center;
        color: #505458;
    }

    .remember {
        margin: 0px 0px 35px 0px;
    }

    .code-box {
        text-align: right;
    }

    .codeimg {
        height: 40px;
    }
</style>